﻿using System;
using System.Collections.Generic;
using Educational.Model.Entity;
using NHibernate;
using NHibernate.Criterion;

namespace Educational.Model.EntityManager
{
    public class PositionManager
    {
        public static void Add(ISession session, Position an)
        {
            session.Save(an);
        }

        public static void Delete(ISession session, Position an)
        {
            session.Delete(an);
        }

        public static void Update(ISession session, Position an)
        {
            session.Update(an);
        }

        public static Position Get(ISession session, int itemId)
        {
            return session.Get<Position>(itemId);
        }

        public static Position Get1(ISession session,int schoolId, int positionId)
        {
            ICriteria c = session.CreateCriteria(typeof(Position));
            ICriteria c1 = c.CreateCriteria("department", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
            ICriteria c2 = c1.CreateCriteria("school", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
                c2.Add(Expression.Eq("schoolId", schoolId));
            c.SetFetchMode("schoolRoleGroup", FetchMode.Eager);
            c.Add(Expression.Eq("positionId", positionId));
            return c.UniqueResult<Position>();
        }

        public static Position Get2(ISession session, int schoolId, int positionId)
        {
            ICriteria c = session.CreateCriteria(typeof(Position));
            ICriteria c1 = c.CreateCriteria("department", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
            ICriteria c2 = c1.CreateCriteria("school", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
            c2.Add(Expression.Eq("schoolId", schoolId));
            c.SetFetchMode("teachers", FetchMode.Eager);
            c.Add(Expression.Eq("positionId", positionId));
            return c.UniqueResult<Position>();
        }

        public static IList<Position> Gets1(ISession session,int schoolId, int[] departmentIds,int rowIndex,int rowCount)
        {
            ICriteria c = session.CreateCriteria(typeof(Position));
            ICriteria c1 = c.CreateCriteria("department", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
            c1.Add(Expression.In("departmentId", departmentIds));
            ICriteria c2= c1.CreateCriteria("school", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
            c2.Add(Expression.Eq("schoolId", schoolId));
            ICriteria c3 = c.CreateCriteria("teachers", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
            c3.SetResultTransformer(NHibernate.CriteriaUtil.DistinctRootEntity);
            c.SetFetchMode("schoolRoleGroup", FetchMode.Eager);
            c.SetFirstResult(rowIndex);
            c.SetMaxResults(rowCount);
            return c.List<Position>();
        }

        public static int Gets1Count(ISession session, int schoolId, int[] departmentIds)
        {
            ICriteria c = session.CreateCriteria(typeof(Position));
            ICriteria c1 = c.CreateCriteria("department", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
            c1.Add(Expression.In("departmentId", departmentIds));
            ICriteria c2 = c1.CreateCriteria("school", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
            c2.Add(Expression.Eq("schoolId", schoolId));
            return (int)c.SetProjection(Projections.Count("positionId")).UniqueResult();
        }
    }
}
